# -*- coding: utf-8 -*-
#
# Copyright (C) 2011  Benjamin Heil
#
# This file is part of Feedmail.
#
# Feedmail is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from django import template
from django.template.loader import render_to_string
from django.conf import settings
from django.utils.encoding import smart_unicode
from account.models import AccountProfile

register = template.Library()

@register.simple_tag
def media_url():
    return settings.MEDIA_URL

@register.simple_tag
def root_url():
    return settings.ROOT_URL

@register.simple_tag
def root_url_wo_slash():
    return settings.ROOT_URL[:len(settings.ROOT_URL) - 1]

@register.simple_tag
def site_title():
    return smart_unicode(settings.SITE_TITLE)

@register.simple_tag
def mail_validated(user):
    profile = AccountProfile.objects.get(user=user)
    p = profile.mail_validated
    return p

@register.simple_tag
def default_from_email():
    return settings.DEFAULT_FROM_EMAIL

@register.filter
def format_date(value):
    return smart_unicode(value.strftime(settings.DATE_FORMAT))

@register.filter
def format_time(value):
    return smart_unicode(value.strftime(settings.TIME_FORMAT))

@register.filter
def format_short_time(value):
    return smart_unicode(value.strftime(settings.TIME_SHORT_FORMAT))

@register.filter
def format_datetime(value):
    return smart_unicode(value.strftime(settings.DATETIME_FORMAT))

@register.filter
def format_short_datetime(value):
    return smart_unicode(value.strftime(settings.DATETIME_SHORT_FORMAT))

@register.tag('hintblock')
def do_hintblock(parser, token):
    nodelist = parser.parse(('endhintblock',))
    parser.delete_first_token()
    return HintBlock(nodelist)

class HintBlock(template.Node):
    def __init__(self, nodelist):
        self.nodelist = nodelist

    def render(self, context):
        output = self.nodelist.render(context)
        return render_to_string('base/hint_block.html', {'text': output},
                                context_instance=context)

@register.tag('errorblock')
def do_errorblock(parser, token):
    nodelist = parser.parse(('enderrorblock',))
    parser.delete_first_token()
    return ErrorBlock(nodelist)

class ErrorBlock(template.Node):
    def __init__(self, nodelist):
        self.nodelist = nodelist

    def render(self, context):
        output = self.nodelist.render(context)
        return render_to_string('base/error_block.html', {'text': output},
                                context_instance=context)
